Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request migrates DB-API DDL tests from system tests to mock server tests, introducing a comprehensive suite in test_dbapi_ddl.py and removing redundant tests from test_dbapi.py. Additionally, test_autocommit_with_json_data is refactored to use an existing table. Feedback on the changes highlights a potential state leak in test_autocommit_with_json_data where self._conn.autocommit is modified but not restored, which could affect subsequent tests sharing the same connection.
Replaces live-instance DDL system tests in `test_dbapi.py` with fast, deterministic in-memory mock server tests in `test_dbapi_ddl.py`. Previously, `test_dbapi.py` dynamically created and dropped ephemeral tables (e.g. `Singers`, `DdlExecute`, `DdlExecuteMany`, `JsonDetails`) against live Cloud Spanner instances to verify client-side DDL buffering and flushing. Each table creation/drop triggered a long-running operation (LRO), adding ~1.5 to 2.0 minutes of latency and causing test flakiness. Changes: - Add `test_dbapi_ddl.py` in `tests/mockserver_tests/` with 9 mock server tests covering immediate autocommit DDL execution, non-autocommit DDL buffering, multi-statement DDL batches, DDL flushing before subsequent queries and DML, and client-side rejection of DDL in `executemany()`. - Remove 7 redundant DDL tests from `tests/system/test_dbapi.py`. - Update `test_autocommit_with_json_data` in `test_dbapi.py` to reuse the existing `all_types` table with row cleanup instead of creating and dropping a dedicated table. - Eliminate all dynamic table churn in DB-API system tests, saving ~1.5 to 2.0 minutes in standard CI while executing the replacement mock tests in < 1 second.
36fbef3 to
5c1c93b
Compare
| self.assertEqual(1, len(execute_requests)) | ||
| self.assertEqual(dml_statement, execute_requests[0].sql) | ||
|
|
||
| connection.commit() |
There was a problem hiding this comment.
Is there any value in moving this commit higher up above the asserts? Gemini spotted a testing gap that we should verify both that the DDL flushed before the query/DML and that commit() didn't re-flush it
Here is the full diff suggested by Gemini
--- a/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_ddl.py
+++ b/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_ddl.py
@@ -126,10 +126,13 @@ class TestDbapiDdl(MockServerTestBase):
cursor.execute(dml_statement)
self.assertEqual(1, len(self.database_admin_service.requests))
- request = self.database_admin_service.requests[0]
- self.assertEqual(self.database.name, request.database)
- self.assertEqual([ddl_statement], request.statements)
self.assertEqual(1, cursor.rowcount)
+ connection.commit()
+
+ self.assertEqual(1, len(self.database_admin_service.requests))
+ request = self.database_admin_service.requests[0]
+ self.assertEqual(self.database.name, request.database)
+ self.assertEqual([ddl_statement], request.statements)
+
execute_requests = [
req
for req in self.spanner_service.requests
if isinstance(req, ExecuteSqlRequest)
]
self.assertEqual(1, len(execute_requests))
self.assertEqual(dml_statement, execute_requests[0].sql)
-
- connection.commit()
finally:
connection.close()
@@ -163,10 +166,13 @@ class TestDbapiDdl(MockServerTestBase):
cursor.executemany(dml_statement, [()])
self.assertEqual(1, len(self.database_admin_service.requests))
- request = self.database_admin_service.requests[0]
- self.assertEqual(self.database.name, request.database)
- self.assertEqual([ddl_statement], request.statements)
self.assertEqual(1, cursor.rowcount)
+ connection.commit()
+
+ self.assertEqual(1, len(self.database_admin_service.requests))
+ request = self.database_admin_service.requests[0]
+ self.assertEqual(self.database.name, request.database)
+ self.assertEqual([ddl_statement], request.statements)
+
batch_requests = [
req
for req in self.spanner_service.requests
if isinstance(req, ExecuteBatchDmlRequest)
]
self.assertEqual(1, len(batch_requests))
self.assertEqual(dml_statement, batch_requests[0].statements[0].sql)
-
- connection.commit()
finally:
connection.close()
@@ -198,9 +204,10 @@ class TestDbapiDdl(MockServerTestBase):
cursor.execute("SELECT 1")
self.assertEqual(1, len(self.database_admin_service.requests))
- request = self.database_admin_service.requests[0]
- self.assertEqual(self.database.name, request.database)
- self.assertEqual([ddl_statement], request.statements)
connection.commit()
+
+ self.assertEqual(1, len(self.database_admin_service.requests))
+ request = self.database_admin_service.requests[0]
+ self.assertEqual(self.database.name, request.database)
+ self.assertEqual([ddl_statement], request.statements)
finally:
connection.close()
Replaces live-instance DDL system tests in
test_dbapi.pywith fast, deterministic in-memory mock server tests intest_dbapi_ddl.py.Previously,
test_dbapi.pydynamically created and dropped ephemeral tables (e.g.Singers,DdlExecute,DdlExecuteMany,JsonDetails) against live Cloud Spanner instances to verify client-side DDL buffering and flushing. Each table creation/drop triggered a long-running operation (LRO), adding ~1.5 to 2.0 minutes of latency and causing test flakiness.Changes:
test_dbapi_ddl.pyintests/mockserver_tests/with 9 mock server tests covering immediate autocommit DDL execution, non-autocommit DDL buffering, multi-statement DDL batches, DDL flushing before subsequent queries and DML, and client-side rejection of DDL inexecutemany().tests/system/test_dbapi.py.test_autocommit_with_json_dataintest_dbapi.pyto reuse the existingall_typestable with row cleanup instead of creating and dropping a dedicated table.